package com.sojson.config.qps.sentinel.origin;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
import com.sojson.util.servlet.ServletUtil;

/**
 * 配置请求来源
 * 
 * @author liu
 * @date 2021-08-25
 */
@Configuration
public class CustomRequestOriginParser {

    /**
     * 获取来源信息</BR>
     * CommonFilter在获取entry之前会使用RequestOriginParser从request中解析来源</BR>
     * 
     * @return
     */
    @Bean
    public RequestOriginParser parseOrigin() {
        return (request -> {
            String remoteAddr = ServletUtil.getTargetIp(request);
            String userAgent = request.getHeader("user-agent");
            return String.join("|", (remoteAddr == null ? "" : remoteAddr), (userAgent == null ? "" : userAgent));
        });
    }

}